package com.business.task;

import java.util.Calendar;
import java.util.Date;

import com.business.bean.oa.ReportDay;

import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.task.RoseTask;
import net.sf.rose.task.Task;
import net.sf.rose.util.DateFormat;
import net.sf.rose.util.RoseRuntimeException;

/**
 * 定时确定上班时间、下班时间、工作时长等
 * <P>
 * 每天计算一次，时间在22点计算
 * 
 * @author fengjian
 *
 */
@Task("员工日报工时计算任务")
public class WorkReportCountTask implements RoseTask {

    private Service service = new Service();
    private BeanDAO dao = new BeanDAO(service);
    private BeanSQL query = dao.getQuerySQL();

    public void setService(Service service) {
        this.service = service;
        this.dao = new BeanDAO(service);
        this.query = dao.getQuerySQL();
    }

    @Override
    public void excute() {
        try {
            Date today = new Date();
            countAllUserDayReport(today); // 计算考勤日报的上班时间
            service.commit();
        } catch (Exception e) {
            throw new RoseRuntimeException(e);
        } finally {
            service.close();
        }
    }

    /**
     * 计算指定日期往前推2天的所有未自动考勤的日报数据
     */
    public void countAllUserDayReport(Date today) {
        service.setCurrentDatabaseName("wison");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.add(Calendar.DAY_OF_MONTH, -2); // 往前推2天
        query.setEntityClass(ReportDay.class);
        String day = DateFormat.format(calendar.getTime(), "yyyy-MM-dd");
        query.setSQL("select t1.* from OA_REPORT_DAY t1 ");
        query.addSql("left join SYS_USER t2 on t1.userID=t2.userID ");
        query.addSql("where t1.usedCount=-1 and t2.status=1 ");
        query.addSql("and t1.calendarDate=?");
        query.addParameter(day);
        /* WorkDayReportService ws = new WorkDayReportService(service);
        List<ReportDay> list = dao.list();
        for (ReportDay report : list) {
            ws.countDayReport(report);
        } */
    }

}
